package com.xlserver.util;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@Component
public class ImageParser {
    //绑定文件上传路径到uploadPath
    @Value("${web.upload-path}")
    private String uploadPath;

    /**
     *上传图片并获得图片地址
     */
    public static String saveImage(MultipartFile image) throws IOException {
        String originalFilename = image.getOriginalFilename();
        String type = FileUtil.extName(originalFilename);
        // 定义一个文件唯一的标识码
        String fileUUID = IdUtil.fastSimpleUUID() + StrUtil.DOT + type;
        File uploadFile = new File("/www/jar/images/" + fileUUID);
        //File uploadFile = new File("C:/images/" + fileUUID);

        System.out.println("uploadFile:"+uploadFile);
        // 上传文件到磁盘
        image.transferTo(uploadFile);
        // 数据库若不存在重复文件，则不删除刚才上传的文件
        return "http://111.231.13.235:8080/headImage/" + fileUUID;
    }
}
